<template>
  <div :class="themeClass">
    <el-container class="app-container">
      <el-header>
        <el-row class="headMenu">
          <el-col
            :span="12"
            class="headColor"
            style="position: relative;">
            <div class="anSteel"/>
            <div style="position: absolute;left: 214px;font-size: 12px;color: #409EFF;">
              <!--<div>{{ webData }}</div>-->
            </div>
          </el-col>
          <el-col :span="12">
            <el-button @click="handleOpenUser">{{username}}</el-button>
          </el-col>
        </el-row>
       
      </el-header>
      <el-row>
        <el-col v-if="isMenu" :span="3" style="width: 200px;height: calc(100vh - 61px);overflow-y: auto" class="barStyel leftMenu">
          <el-menu
            v-if="levelList.length>0"
            :default-active="currentPath"
            mode="vertical"
            unique-opened
            router
            background-color="#1a367a"
            active-text-color="#3391ff"
            style="height: calc(100vh - 61px);">
            <div v-for="(item, index) in levelList" :key="index + 'menuV'">
              <el-submenu
                v-if="item.children != undefined && item.children.length > 0"
                :index="item.id">
                <template slot="title">
                  <i :class="item.icon"/>
                  <span>{{ item.label }}</span>
                </template>
                <el-menu-item
                  v-for="(itemChild, indexChild) in item.children"
                  :key="indexChild + 'menuC'"
                  :index="itemChild.path">
                  <span slot="title">{{ itemChild.label }}</span>
                </el-menu-item>
              </el-submenu>

              <el-menu-item v-else :index="item.path">
                <template slot="title">
                  <i :class="item.icon"/>
                  <span>{{ item.label }}</span>
                </template>
              </el-menu-item>
            </div>
          </el-menu>
        </el-col>
        <el-col
          :span="isMenu ? 21 : 24"
          :style="isMenu ? 'width: calc(100% - 200px);' : ''"
          style="height: calc(100vh - 61px);overflow-y: auto;padding: 10px;"
          class="xw_contain barStyel">
          <nuxt />
        </el-col>
      </el-row>
    </el-container>


    <el-dialog
      :visible.sync="dialogVisible"
      title="选择用户"
      width="30%">
      <el-select v-model="userId" placeholder="请选择用户" style="width: 100%">
        <el-option
          v-for="item in userInfo"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmitUserInfo">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
import _ from "lodash";
export default {
  head() {
    return {
      title: this.title,
      meta: [
        {
          name: "keywords",
          content: "首页"
        }
      ]
    };
  },
  data() {
    return {
      isMenu: true,
      theme: "them-deep",
      username: "请选择用户",
      user: "",
      menu: [],
      menuV: [],
      levelList: [],
      level: 0,
      currentPath: "",
      isHeadData: "",
      userInfo: [
        {
          name: "销售",
          id: 60049195,
          menu: [
            {
              id: "24",
              label: "销售公司",
              icon: "el-icon-s-grid",
              path: "/sale",
              children: [
                {
                  id: "100",
                  label: "首页",
                  icon: "el-icon-s-grid",
                  path: "/sale/home"
                },
                {
                  id: "toBehandle",
                  label: "待处理评审",
                  icon: "el-icon-s-grid",
                  path: "/sale/toBehandle"
                },
                {
                  id: "toBeSent",
                  label: "已发送评审",
                  icon: "el-icon-s-grid",
                  path: "/sale/toBeSent"
                },
                {
                  id: "history",
                  label: "历史评审",
                  icon: "el-icon-s-grid",
                  path: "/sale/history"
                }
              ]
            }
          ]
        },
        {
          name: "标准科",
          id: 60071184,
          menu: [
            {
              id: "25",
              label: "技术中心标准科目",
              icon: "el-icon-s-grid",
              path: "/standard",
              children: [
                {
                  id: "200",
                  label: "首页",
                  icon: "el-icon-s-grid",
                  path: "/standard/home"
                },
                {
                  id: "pendingOrderReview",
                  label: "待审核订单",
                  icon: "el-icon-s-grid",
                  path: "/standard/pendingOrderReview"
                },
                {
                  id: "approvedOrder",
                  label: "已审核订单",
                  icon: "el-icon-s-grid",
                  path: "/standard/approvedOrder"
                },
                {
                  id: "orderRecord",
                  label: "修改意见订单记录",
                  icon: "el-icon-s-grid",
                  path: "/standard/orderRecord"
                }
              ]
            }
          ]
        },
        {
          name: "评审人员",
          id: 60048825,
          menu: [
            {
              id: "34",
              label: "技术中心/总工办",
              icon: "el-icon-s-grid",
              path: "/center",
              children: [
                {
                  id: "301",
                  label: "首页",
                  path: "/center/home"
                },
                {
                  id: "302",
                  label: "待评审订单",
                  path: "/center/toBeReviewed"
                },
                {
                  id: "303",
                  label: "已评审订单",
                  path: "/center/toBeSent"
                }
              ]
            }
          ]
        },
        {
          name: "主任",
          id: 60057812,
          menu: [
            {
              id: "40",
              label: "技术中心/总工办",
              icon: "el-icon-s-grid",
              path: "/center",
              children: [
                {
                  id: "41",
                  label: "首页",
                  path: "/center/home"
                },
                {
                  id: "42",
                  label: "待评审订单",
                  path: "/center/toBeReviewed"
                },
                {
                  id: "43",
                  label: "已评审订单",
                  path: "/center/toBeSent"
                }
              ]
            }
          ]
        }
      ],
      userId: "",
      dialogVisible: false
    };
  },
  computed: {
    themeClass() {
      return `theme-${this.theme}`;
    }
  },
  watch: {
    $route: {
      handler: function(val, oldVal) {
        if (val.path === "/") {
          this.currentPath = val.path;
        } else {
          if (global.localStorage !== undefined) {
            global.localStorage.setItem("path", val.path);
          }
          this.currentPath = val.path;
        }
      },
      // 深度观察监听
      deep: true,
      immediate: true
    }
  },
  mounted() {
    this.username = localStorage.getItem("userName")
      ? localStorage.getItem("userName")
      : "请选择用户";
    this.levelList = localStorage.getItem("userMenu")
      ? JSON.parse(localStorage.getItem("userMenu"))
      : [];
  },
  methods: {
    // 修改主题
    changeThem(command) {
      this.theme = command;
      window.localStorage.setItem("pageThem", command);
      window.location.reload();
    },
    menuSelect(key, flag) {
      let ind = _.findIndex(this.menu, o => {
        return o.path === key;
      });
      for (var i = 0; i < this.menu.length; i++) {
        const d = JSON.stringify(this.menu[i]);
        ind = d.indexOf(key);
        if (ind > -1) {
          ind = i;
          break;
        }
      }
      if (ind > -1) {
        this.menuV = this.levelList[ind].children;
      } else {
        this.menuV = [];
      }
    },
    // 递归实现
    // @leafLabel  查找的id，
    // @nodes   原始Json数据
    // @path    供递归使用
    findPathByleafLabel(leafLabel, nodes, path) {
      if (path === undefined) {
        path = [];
      }
      for (var i = 0; i < nodes.length; i++) {
        var tmpPath = path.concat();
        tmpPath.push(nodes[i]);
        if (leafLabel === nodes[i].path) {
          return tmpPath;
        }
        if (nodes[i].children) {
          var findResult = this.findPathByleafLabel(
            leafLabel,
            nodes[i].children,
            tmpPath
          );
          if (findResult) {
            return findResult;
          }
        }
      }
    },
    // 递归实现
    // @nodes   原始Json数据
    findRole(nodes) {
      var newMenu = [];
      for (var i = 0; i < nodes.length; i++) {
        if (
          nodes[i].role === undefined ||
          nodes[i].role.indexOf(this.$store.state.user.role) > -1
        ) {
          // if (nodes[i].role !== 'admin') {
          newMenu.push(nodes[i]);
          if (nodes[i].children) {
            nodes[i].children = this.findRole(nodes[i].children);
            if (nodes[i].children.length === 0) {
              newMenu.pop();
            }
            if (nodes[i].children.length === 1) {
              nodes[i].path = nodes[i].children[0].path;
              delete nodes[i].children;
            }
          }
        }
      }
      return newMenu;
    },
    handleOpenUser() {
      this.dialogVisible = true;
    },
    handleSubmitUserInfo() {
      if (this.userId) {
        let obj = this.userInfo.filter(item => {
          return item.id == this.userId;
        });
        this.levelList = obj[0].menu;
        this.username = obj[0].name;

        localStorage.setItem("userName", obj[0].name);
        localStorage.setItem("userMenu", JSON.stringify(obj[0].menu));
        this.$store.dispatch("setUserID", obj[0].id);
      }
      this.dialogVisible = false;
    }
  }
};
</script>
<style>
html,
body {
  font-family: "Source Sans Pro", -apple-system, BlinkMacSystemFont, "Segoe UI",
    Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  word-spacing: 1px;
  -ms-text-size-adjust: 100%;
  -webkit-text-size-adjust: 100%;
  -moz-osx-font-smoothing: grayscale;
  -webkit-font-smoothing: antialiased;
  box-sizing: border-box;
}

*,
*:before,
*:after {
  box-sizing: border-box;
  margin: 0;
}
.xw_nercar {
  float: right;
  padding: 14px 7px 0px 10px;
}
.el-header {
  padding: 0px 10px 0px 0px !important;
}
.anSteel {
  float: left;
  width: 200px;
  height: 100%;
  background-image: url("@/assets/img/nercar.png");
  background-repeat: no-repeat;
  background-size: 80%;
  background-position: center;
}
</style>
